home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 1 / CU Amiga Magazine CD-ROM Special Edition (1995)(EMAP Images)(GB)[Issue 1995-11].iso / Aminet / comm / tcp / ATCP_sdk_40_gc.lha / AmiTCP-4.0-gcc / src / rpclib / getrpcent.c < prev    next >
C/C++ Source or Header  |  1995-03-17  |  4KB  |  243 lines

  1. /*
  2.  *    $Id: getrpcent.c,v 4.2 1994/09/29 23:48:50 jraja Exp $
  3.  *
  4.  *    Copyright © 1994 AmiTCP/IP Group,
  5.  *             Network Solutions Development Inc.
  6.  *             All rights reserved.
  7.  */
  8.  
  9. /* @(#)getrpcent.c      2.2 88/07/29 4.0 RPCSRC */
  10. #if !defined(lint) && defined(SCCSIDS)
  11. static    char sccsid[] = "@(#)getrpcent.c 1.9 87/08/11  Copyr 1984 Sun Micro";
  12. #endif
  13.  
  14. /*
  15.  * Copyright (c) 1985 by Sun Microsystems, Inc.
  16.  */
  17.  
  18. #include <sys/param.h>
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <sys/types.h>
  22. #include <rpc/rpc.h>
  23. #include <netdb.h>
  24. #include <sys/socket.h>
  25.  
  26. /*
  27.  * Internet version.
  28.  */
  29. struct rpcdata {
  30. #ifdef USE_DOSIO /* AMIGA specific */
  31.     BPTR    rpcf;
  32. #else
  33.     FILE    *rpcf;
  34. #endif
  35. #ifndef AMIGA
  36.     char    *current;
  37.     int    currentlen;
  38. #endif
  39.     int    stayopen;
  40. #define MAXALIASES    35
  41.     char    *rpc_aliases[MAXALIASES];
  42.     struct    rpcent rpc;
  43.     char    line[BUFSIZ+1];
  44.     char    *domain;
  45. } *rpcdata;
  46.  
  47. static struct rpcdata *_rpcdata(void);
  48.  
  49. /*
  50.  * The ent-functions have been made static, since the database
  51.  * implementation should be hidden from the applications (J.R.)
  52.  */
  53. static void setrpcent(int f);
  54. static void endrpcent(void);
  55. static struct rpcent * getrpcent(void);
  56. static struct rpcent * interpret(char *val, size_t len);
  57.  
  58. static    char *index();
  59.  
  60. #if defined(AMITCP)
  61. static char RPCDB[] = "AmiTCP:db/rpc";
  62. #else
  63. static char RPCDB[] = "/etc/rpc";
  64. #endif
  65.  
  66. static struct rpcdata *
  67. _rpcdata(void)
  68. {
  69.     register struct rpcdata *d = rpcdata;
  70.  
  71.     if (d == 0) {
  72.         d = (struct rpcdata *)mem_calloc(1, sizeof (struct rpcdata));
  73.         rpcdata = d;
  74.     }
  75.     return (d);
  76. }
  77.  
  78. struct rpcent *
  79. getrpcbynumber(number)
  80.     register int number;
  81. {
  82.     register struct rpcdata *d = _rpcdata();
  83.     register struct rpcent *p;
  84.  
  85.     if (d == 0)
  86.         return (0);
  87.     setrpcent(0);
  88.     while (p = getrpcent()) {
  89.         if (p->r_number == number)
  90.             break;
  91.     }
  92.     endrpcent();
  93.     return (p);
  94. }
  95.  
  96. struct rpcent *
  97. getrpcbyname(name)
  98.     char *name;
  99. {
  100.     struct rpcent *rpc;
  101.     char **rp;
  102.  
  103.     setrpcent(0);
  104.     while(rpc = getrpcent()) {
  105.         if (strcmp(rpc->r_name, name) == 0)
  106.             return (rpc);
  107.         for (rp = rpc->r_aliases; *rp != NULL; rp++) {
  108.             if (strcmp(*rp, name) == 0)
  109.                 return (rpc);
  110.         }
  111.     }
  112.     endrpcent();
  113.     return (NULL);
  114. }
  115.  
  116. static void
  117. setrpcent(int f)
  118. {
  119.     register struct rpcdata *d = _rpcdata();
  120.  
  121.     if (d == 0)
  122.         return;
  123.     if (d->rpcf == NULL)
  124. #ifdef USE_DOSIO
  125.         d->rpcf = Open((STRPTR)RPCDB, MODE_OLDFILE);
  126. #else
  127.         d->rpcf = fopen(RPCDB, "r");
  128. #endif
  129.     else
  130.         rewind(d->rpcf);
  131. #ifndef AMIGA
  132.     if (d->current)
  133.         mem_free(d->current, d->currentlen);
  134.     d->current = NULL;
  135. #endif
  136.     d->stayopen |= f;
  137. }
  138.  
  139. static void
  140. endrpcent(void)
  141. {
  142.     register struct rpcdata *d = _rpcdata();
  143.  
  144.     if (d == 0)
  145.         return;
  146. #ifndef AMIGA
  147.     if (d->current && !d->stayopen) {
  148.         mem_free(d->current, d->currentlen);
  149.         d->current = NULL;
  150.     }
  151. #endif
  152.     if (d->rpcf && !d->stayopen) {
  153.         fclose(d->rpcf);
  154.         d->rpcf = NULL;
  155.     }
  156. }
  157.  
  158. static struct rpcent *
  159. getrpcent(void)
  160. {
  161.     register struct rpcdata *d = _rpcdata();
  162.  
  163.     if (d == 0)
  164.         return(NULL);
  165.     if (d->rpcf == NULL &&
  166. #ifdef USE_DOSIO
  167.         (d->rpcf = Open((STRPTR)RPCDB, MODE_OLDFILE))
  168. #else
  169.         (d->rpcf = fopen(RPCDB, "r"))
  170. #endif
  171.         == NULL)
  172.         return (NULL);
  173.     if (fgets(d->line, BUFSIZ, d->rpcf) == NULL)
  174.         return (NULL);
  175.     return interpret(d->line, strlen(d->line));
  176. }
  177.  
  178. static struct rpcent *
  179. interpret(char *val, size_t len)
  180. {
  181.     register struct rpcdata *d = _rpcdata();
  182.     char *p;
  183.     register char *cp, **q;
  184.  
  185.     if (d == 0)
  186.         return NULL;
  187.     strncpy(d->line, val, len);
  188.     p = d->line;
  189.     d->line[len] = '\n';
  190.     if (*p == '#')
  191.         return (getrpcent());
  192.     cp = index(p, '#');
  193.     if (cp == NULL)
  194.     {
  195.         cp = index(p, '\n');
  196.         if (cp == NULL)
  197.             return (getrpcent());
  198.     }
  199.     *cp = '\0';
  200.     cp = index(p, ' ');
  201.     if (cp == NULL)
  202.     {
  203.         cp = index(p, '\t');
  204.         if (cp == NULL)
  205.             return (getrpcent());
  206.     }
  207.     *cp++ = '\0';
  208.     /* THIS STUFF IS INTERNET SPECIFIC */
  209.     d->rpc.r_name = d->line;
  210.     while (*cp == ' ' || *cp == '\t')
  211.         cp++;
  212.     d->rpc.r_number = atoi(cp);
  213.     q = d->rpc.r_aliases = d->rpc_aliases;
  214.     cp = index(p, ' ');
  215.     if (cp != NULL)
  216.         *cp++ = '\0';
  217.     else
  218.     {
  219.         cp = index(p, '\t');
  220.         if (cp != NULL)
  221.             *cp++ = '\0';
  222.     }
  223.     while (cp && *cp) {
  224.         if (*cp == ' ' || *cp == '\t') {
  225.             cp++;
  226.             continue;
  227.         }
  228.         if (q < &(d->rpc_aliases[MAXALIASES - 1]))
  229.             *q++ = cp;
  230.         cp = index(p, ' ');
  231.         if (cp != NULL)
  232.             *cp++ = '\0';
  233.         else
  234.         {
  235.             cp = index(p, '\t');
  236.             if (cp != NULL)
  237.                 *cp++ = '\0';
  238.         }
  239.     }
  240.     *q = NULL;
  241.     return (&d->rpc);
  242. }
  243.